package zj.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.model.Org;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class GsSelectAdapter extends BaseAdapter{

    public List<Org> list;
    public Context context;
    public LayoutInflater inflater;

    public boolean isEdit = false;

    public Map<Integer,Boolean> selectMap = new HashMap<Integer,Boolean>();
    public Map<Integer,Org> selectData = new HashMap<Integer,Org>();
    public GsSelectAdapter(List<Org> list, Context context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_select_item, null);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            viewHolder.view = convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }




        if(selectMap.get(position) != null){
            if(selectMap.get(position)){
                viewHolder.view.setBackgroundColor(Color.parseColor("#e8e8e8"));

            }else{
                viewHolder.view.setBackgroundColor(Color.WHITE);

            }
        }

        viewHolder.name_tv.setText(list.get(position).getOrgName());

        return convertView;
    }
    private static class ViewHolder {
        TextView name_tv;
        View view;

    }

    public void setDataChange(List<Org> list){


        this.list = list;
        notifyDataSetChanged();
    }
    public void setSelect(int position){



      if(selectMap.get(position) == null){

          selectMap.put(position,true);
          selectData.put(position,list.get(position));

      }else{
          Boolean b =   selectMap.get(position);
          if(b){
              selectMap.put(position,false);
              selectData.remove(position);
          }else{
              selectMap.put(position,true);
              selectData.put(position,list.get(position));


          }
      }

        notifyDataSetChanged();

    }

    public Map<Integer,Org> getSelectMap(){

        return selectData;
    }

}
